package com.lj.demo.orderService.service.impl;

import com.lj.demo.commonutils.daoVo.CourseWebVoOne;
import com.lj.demo.commonutils.daoVo.UcenterMemberVo;
import com.lj.demo.orderService.client.EduClient;
import com.lj.demo.orderService.client.UcenterClient;
import com.lj.demo.orderService.entity.TOrder;
import com.lj.demo.orderService.mapper.TOrderMapper;
import com.lj.demo.orderService.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.demo.orderService.utils.OrderNoUtil;
import com.lj.demo.servicebase.exception.AException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author liujiang
 * @since 2021-01-08
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String saveOrder(String courseId, String memberIdByJwtToken) {
        //根据课程id获取课程信息
        CourseWebVoOne course = eduClient.getDto(courseId);

        //根据用户id获取用户信息
        UcenterMemberVo ucenterMemberVo = ucenterClient.getInfoUc(memberIdByJwtToken);
        if(StringUtils.isEmpty(ucenterMemberVo.getId())){
            throw new AException(20001,"尚未登录，请先登录");
        }
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo()); //生成订单号
        order.setCourseId(courseId);  //课程id
        order.setCourseTitle(course.getTitle()); //课程标题
        order.setCourseCover(course.getCover()); //课程封面
        order.setTeacherName(course.getTeacherName()); //老师名称
        order.setTotalFee(course.getPrice());  //课程价格
        order.setMemberId(memberIdByJwtToken); //用户id
        order.setMobile(ucenterMemberVo.getMobile()); //用户手机号
        order.setNickname(ucenterMemberVo.getNickname()); // 用户名
        order.setStatus(0); //支付状态 0未支付 1已支付
        order.setPayType(1); // 支付方式
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
